﻿<template>
    <el-container>
        <el-header>
            <div class="left-panel">
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon
                        class="vab-dropdown"
                        :class="{ 'vab-dropdown-active': fold }"
                        icon="arrow-up-s-line"
                    />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px">
                    <!-- <el-form-item label="营业时间" label-width="100px">
                        <el-date-picker
                            style="width: 300px"
                            v-model="queryList.consumeTimes"
                            type="datetimerange"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            format="YYYY-MM-DD HH:mm:ss"
                            range-separator="-"
                            unlink-panels="true"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                            :default-time="defaultTime"
                        />
                    </el-form-item>
                    <el-form-item label="点单商品编码" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.goodsCode"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="点单商品名称" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.goodsName"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="点单商品规格" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.standard"
                            style="width: 300px"
                        />
                    </el-form-item>-->
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable
                ref="mytable"
                :queryType="'store-inventory/pages'"
                :queryList="queryList"
                :column="columnList"
                :orderList="OrderList"
                :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange"
                row-key="storeInventoryId"
                key="storeInventoryId"
                stripe
                show-summary
                remoteSummary
            >
                <template #status="{row}">{{row.statusDesc}}</template>
                <template #stockStatus="{row}">{{row.stockStatusDesc}}</template>
                <el-table-column label="操作" fixed="right" align="center" min-width="180">
                    <template #default="scope">
                        <el-button-group>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opShow(scope.row, scope.$index)"
                            >查看</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="openInStock(scope.row, scope.$index)"
                            >入库明细</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="openOutStock(scope.row, scope.$index)"
                            >出库明细</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-if="scope.row.status==0"
                                @click="opEdit(scope.row, scope.$index)"
                            >盘点</el-button>
                            <sc-export
                                :queryType="`store-inventory/export/${scope.row.storeInventoryId}`"
                                :fileName="'盘点单'"
                                :isTest="true"
                                :isText="true"
                                showText="导出"
                            />
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-if="scope.row.status==1&&scope.row.stockStatus==0"
                                @click="opStocks(scope.row, scope.$index)"
                            >生成库存订单</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <save-dialog
        v-if="dialogSave"
        ref="saveDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogSave = false"
    ></save-dialog>
</template>

<script>
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import saveDialog from "./save";
import config from "@/config";
export default {
    name: "storeInventoryStock",
    components: {
        publicjs,
        saveDialog,
    },
    data() {
        return {
            dialogSave: false,
            showCondition: false,
            selectionRows: [],
            pkList: ["itemCode"],
            columnList: [
                {
                    prop: "storeName",
                    label: "店铺名称",
                    width: "160",
                    sortable: false,
                },
                {
                    prop: "warehouseName",
                    label: "仓库名称",
                    width: "160",
                    sortable: false,
                },
                {
                    prop: "storeInventoryCode",
                    label: "盘点单号",
                    width: "180",
                    sortable: false,
                },
                {
                    prop: "startDate",
                    label: "盘点开始时间",
                    width: "240",
                    sortable: false,
                },
                {
                    prop: "endDate",
                    label: "盘点结束时间",
                    width: "240",
                    sortable: false,
                },
                {
                    prop: "status",
                    label: "盘点状态",
                    width: "160",
                    sortable: false,
                },
                {
                    prop: "stockStatus",
                    label: "是否生成库存",
                    width: "160",
                    sortable: false,
                },
                {
                    prop: "remarks",
                    label: "备注",
                    width: "110",
                    sortable: false,
                },
            ],
            OrderList: { storeInventoryId: "DESC" },
            queryList: {
                materialTypeIds: [],
            },
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsState: [],
            materialTypeTree: [],
            optionsBrand: [],
            optionsUnit: [],
            goodTypeTree: [],
            isShow: true, //是否显示
            defaultTime: [
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 2, 1, 23, 59, 59),
            ],
        };
    },
    methods: {
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add");
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            console.log(this.$refs);
            this.$nextTick(() => {
                this.$refs.saveDialog.setData(row);
                this.$refs.saveDialog.open("Edit");
            });
        },
        //查看
        opShow(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.setData(row);
                this.$refs.saveDialog.open("Show");
            });
        },
        //生成库存订单
        async opStocks(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    `store-inventory/stocks/${row.storeInventoryId}`,
                    {}
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList = {};
        },
        //查看入库明细
        async openInStock(row) {
            this.$router.push({
                path: "/inStock/index",
                query: {fkOrderCode:row.storeInventoryCode},
            });
        },
         //查看出库明细
         async openOutStock(row) {
            this.$router.push({
                path: "/outStock",
                query: {fkOrderCode:row.storeInventoryCode},
            });
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["Status"] }
                );
                if (returnValue.status == 200) {
                    this.optionsState = returnValue.response.find(
                        (p) => p.code == "Status"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "material-type/tree",
                    { status: 1 }
                );
                if (returnValue.status == 200) {
                    this.materialTypeTree = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/dic-options",
                    { ParentBaseDictionaryCodes: ["001", "002"] }
                );
                if (returnValue.status == 200) {
                    this.optionsBrand = returnValue.response.find(
                        (p) => p.code == "001"
                    ).optionsDetails;
                    this.optionsUnit = returnValue.response.find(
                        (p) => p.code == "002"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "pos-sale/org-type",
                    {}
                );
                if (returnValue.status == 200) {
                    this.goodTypeTree = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        formatDate(date, type = 0) {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, "0");
            const day = String(date.getDate()).padStart(2, "0");
            const hours = String(date.getHours()).padStart(2, "0");
            const minutes = String(date.getMinutes()).padStart(2, "0");
            const seconds = String(date.getSeconds()).padStart(2, "0");

            return type == 0
                ? `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
                : `${year}-${month}-${day}`;
        },
    },
    async mounted() {
        var orgType = tool.data.get("USER_INFO").AccountType;
        if (orgType == 1) {
            this.isShow = true;
        }
        // 获取当前日期
        const now = new Date();
        // 创建前一天的日期对象
        let previousDay = new Date(now);
        previousDay.setDate(now.getDate() - 1); // 设置为前一天
        // 将时间设置为 12:00 PM
        previousDay.setHours(12, 0, 0, 0); // 设置时、分、秒、毫秒为 12:00:00.000
        this.queryList.consumeTimes = [previousDay, now];
        console.log(orgType, this.isShow);
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
